import { reqShopComments, reqShopGoods } from "../api/index"
import { RECEIVE_COMMENTS, RECEIVE_GOODS, INCREASE_FOOD_COUNT, DECREASE_FOOD_COUNT, CLEAR_CART } from "./mutation-types"

export default {
  //异步获取商家评价
  async getShopComments(context, callback) {
    let result = await reqShopComments()
    if (result.code === 0) {
      const comments = result.data
      context.commit(RECEIVE_COMMENTS, { comments })
      callback && callback()
    }
  },
  //异步获取商家商品
  async getShopGoods(context, callback) {
    let result = await reqShopGoods()
    if (result.code === 0) {
      const goods = result.data
      context.commit(RECEIVE_GOODS, { goods })
      callback && callback()
    }
  },

  //修改购物车中某种食物的数量
  changeFoodCount(context, { isPlus, food }) {
    if (isPlus) {
      context.commit(INCREASE_FOOD_COUNT, { food })
    } else {
      context.commit(DECREASE_FOOD_COUNT, { food })
    }
  },

  //清空购物车
  clearCart(context) {
    context.commit(CLEAR_CART)
  },
}
